use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

fn main1() {
    let mut socket = 
        SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
        assert_eq!("127.0.0.1:8080".parse(), Ok(socket));
    let socket_v6 = 
        SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 65535, 0, 1)), 8080);


    //返回IP
    assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));

    //设置IP：修改socketAddr对应的Ip
    socket.set_ip(IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));
    assert_eq!(socket.ip(), IpAddr::V4(Ipv4Addr::new(10, 10, 0, 1)));

    //返回端口
    assert_eq!(socket.port(), 8080);

    // 设置端口：修改socketAddr对应的端口
    socket.set_port(1025);
    assert_eq!(socket.port(), 1025);


    // 判断是否为ipv4
    assert_eq!(socket.is_ipv4(), true);
    assert_eq!(socket_v6.is_ipv4(), false);

    // 判断是否为ipv6
    assert_eq!(socket.is_ipv6(), false);
    assert_eq!(socket_v6.is_ipv6(), true);
}

use std::io::TcpStream;
use std::string::String;
 
fn main() {
	let mut socket = TcpStream::connect("www.baidu.com", 80).unwrap();//连接baidu
	socket.write(b"GET / HTTP/1.0\n\n");  //获取发送网页
	let response = socket.read_to_end(); //得到结果
	let s = String::from_utf8(response.ok().unwrap()); //转换结果为字符串.
	println!("{}",s);
}